/** 
 * @author: Aristo Ingenieros SRL.
 * @description: 
 *              - Al inicio carga la configuracion del modulo contable
 *              - Los link indican la carga de un item del menu
 *              - Aqui esta implementado cerrar sesion json y servicio
 */

$(function() {
    load = new ajaxLoader();

    loadConfigDialogsSystem();
    fverificaLogin();

    $("#aristo-main").effect("slide", "slow", 500);

    /* configuraciones de urls */
    //menu archivo
    $("#linkConfCuentaContable").click(eventoLoadPanelCCC);
    $("#linkConfCuentaPresupuestal").click(eventoLoadPanelCCP);
    $("#linkConfPersonal").click(eventoLoadPanelCP);
    $("#linkConfConcepto").click(eventoLoadPanelCC);
    $("#linkCerrarSesion").click(eventoCerrarSesion);

    //nota contable
    $("#linkConfNotaContable").click(eventoLoadPanelCNC);

    //recibo de ingreso
    $("#linkRegRecibo").click(eventoLoadPanelRI);
    $("#linkAnularRecibo").click(eventoLoadPanelAR);

    //orden de compra
    $("#linkRegOrdenCompra").click(eventoLoadPanelOC);
    $("#linkComprobantePen").click(eventoLoadPanelCPP);
    $("#linkAnularEgreso").click(eventoLoadPanelAE);
    
    $("#linkImpLibroDiario").click(eventoImprimirLD);
    $("#linkImpLibroCaja").click(eventoImprimirLC);
    $("#linkImpLibroBanco").click(eventoImprimirLB);
    $("#linkImpLibroMayor").click(eventoImprimirLM);
    $("#linkImpLibroBGeneral").click(eventoImprimirBGeneral);

    if (load)
        load.remove();
});

function eventoCerrarSesion() {
    $("#dialog-confirm").dialog("open");
    return false;
}

function consumirCerrarSesion() {
    var json = new Object();
    json.mod = 'LOGOUT';

    var jsonCerrarSesion = {
        data: json,
        type: "POST",
        dataType: "json",
        url: URI_ARISTO,
        success: vlfResultCerrarSesion,
        error: ferror
    };

    $.ajax(jsonCerrarSesion);
}

function vlfResultCerrarSesion(data) {
    if (data.estado === 'SUCCESS') {
        fverificaLogin();
    } else {
        mensajeAristo('warning.png', data.descripcion);
    }
}

// verificamos si la sesion esta activa
function fverificaLogin() {
    var json = new Object();
    json.mod = 'SESSION';

    var consumirJson = {
        data: json,
        type: 'GET',
        dataType: 'json',
        url: URI_ARISTO,
        success: verificaUbicacion,
        error: ferror
    };

    $.ajax(consumirJson);
}

function verificaUbicacion(data) {
    if (data.estado === 'SUCCESS') {
        var modulo = data.module;
        if (modulo === 'SECRETARIA') {
            $(location).attr('href', URI_SECRETARIA);
        } else if (modulo === 'NULL') {
            $(location).attr('href', URI_LOGIN);
        }
    } else {
        mensajeAristo('warning.png', data.descripcion);
    }
}

/**
 * 
 * @type GRID PANEL CONFIGURAR CUENTA CONTABLE
 */
var configDG_pccc_value = "";
var configDG_pccc = {
    height: 362,
    autoLoad: false,
    pagination: false,
    onClickRow: function(row) {
        configDG_pccc_value = "";
        $(row.cells).each(function(k, v) {
            //console.log("columna: " + k + ", valor: " +  v.innerHTML);
            if (k == 1) {
                configDG_pccc_value += v.innerHTML + ": ";
            } else if (k == 2) {
                configDG_pccc_value += v.innerHTML;
            }
            //console.log("value: "+configDG_pccc_value);
            //envia para eliminar
            $("#dialog-descripcion-cuenta").html(configDG_pccc_value);
        });
    },
    mapper: [
        {name: 'id', title: '&nbsp;', width: "6%", align: 'center', render: function(v) {
                return "<a href='#'><img src='ui/img/edit_16.png' onclick='editarFila_ccc(" + v + "); return false;'/></a>&nbsp;<a href='#'><img src='ui/img/delete.png' onclick='eliminarFila_ccc(" + v + "); return false;'/></a>";
            }},
        {name: 'seccion', title: 'SECCION', width: "14%", align: 'center'},
        {name: 'descripcion', title: 'DESCRIPCION', width: "40%"},
        {name: 'padre', title: 'PADRE', width: "40%"}
    ]
};

/**
 * 
 * @type GRID PANEL CONFIGURAR CUENTA PRESUPUESTAL
 */
var configDG_pccp_value = "";
var configDG_pccp = {
    height: 362,
    autoLoad: false,
    pagination: false,
    onClickRow: function(row) {
        configDG_pccp_value = "";
        $(row.cells).each(function(k, v) {
            //console.log("columna: " + k + ", valor: " +  v.innerHTML);
            if (k == 1) {
                configDG_pccp_value += v.innerHTML + ": ";
            } else if (k == 2) {
                configDG_pccp_value += v.innerHTML;
            }
            //console.log("value: "+configDG_pccc_value);
            //envia para eliminar
            $("#dialog-descripcion-cuenta").html(configDG_pccp_value);
        });
    },
    mapper: [
        {name: 'id', title: '&nbsp;', width: "6%", align: 'center', render: function(v) {
                return "<a href='#'><img src='ui/img/edit_16.png' onclick='editarFila_ccp(" + v + "); return false;'/></a>&nbsp;<a href='#'><img src='ui/img/delete.png' onclick='eliminarFila_ccp(" + v + "); return false;'/></a>";
            }},
        {name: 'seccion', title: 'SECCION', width: "14%", align: 'center'},
        {name: 'descripcion', title: 'DESCRIPCION', width: "40%"},
        {name: 'padre', title: 'PADRE', width: "40%"}
    ]
};

/**
 * 
 * @type GRID PANEL CONFIGURAR PERSONAL
 */
var configDG_pcp_value = "";
var tipo_CP = "";
var configDG_pcp = {
    height: 362,
    autoLoad: false,
    pagination: false,
    onClickRow: function(row) {
        configDG_pcp_value = "";
        $(row.cells).each(function(k, v) {
            //console.log("columna: " + k + ", valor: " +  v.innerHTML);
            if (k == 1) {
                configDG_pcp_value += v.innerHTML + " ";
            } else if (k == 2) {
                configDG_pcp_value += v.innerHTML;
            } else if (k == 3) {
                configDG_pcp_value = v.innerHTML + ": " + configDG_pcp_value;
                tipo_CP = v.innerHTML;
            }
            $("#dialog-nombre-apellido_ep").html(configDG_pcp_value);
        });
    },
    mapper: [
        {name: 'id', title: '&nbsp;', width: "6%", align: 'center', render: function(v) {
                return "<a href='#'><img src='ui/img/edit_16.png' onclick='editarFila_cp(" + v + "); return false;'/></a>&nbsp;<a href='#'><img src='ui/img/delete.png' onclick='eliminarFila_cp(" + v + "); return false;'/></a>";
            }},
        {name: 'nombres', title: 'NOMBRES', width: "30%"},
        {name: 'apellidos', title: 'APELLIDOS', width: "40%"},
        {name: 'tipo', title: 'TIPO', width: "18%", align: 'center'}
    ]
};

/**
 * 
 * @type GRID PANEL CONFIGURAR CONCEPTO
 */
var configDG_pcc_value = "";
var configDG_pcc = {
    height: 362,
    autoLoad: false,
    pagination: false,
    onClickRow: function(row) {
        configDG_pcc_value = "";
        $(row.cells).each(function(k, v) {
            //console.log("columna: " + k + ", valor: " +  v.innerHTML);
            if (k == 1) {
                configDG_pcc_value += v.innerHTML + " ";
            } else if (k == 2) {
                configDG_pcc_value += v.innerHTML;
            } else if (k == 3) {
                configDG_pcc_value = v.innerHTML + ": " + configDG_pcc_value;
            }
            $("#dialog-descripcion-concepto").html(configDG_pcc_value);
        });
    },
    mapper: [
        {name: 'id', title: '&nbsp;', width: "6%", align: 'center', render: function(v) {
                return "<a href='#'><img src='ui/img/edit_16.png' onclick='editarFila_cc(" + v + "); return false;'/></a>&nbsp;<a href='#'><img src='ui/img/delete.png' onclick='eliminarFila_cc(" + v + "); return false;'/></a>";
            }},
        {name: 'tipo', title: 'TIPO', width: "14%", align: "center"},
        {name: 'concepto', title: 'CONCEPTO', width: "60%"},
        {name: 'monto', title: 'MONTO', width: "22%", align: "right"}
    ]
};

/**
 * 
 * @type GRID PANEL CONFIGURAR NOTA
 */
var configDG_pcn_value = "";
var configDG_pcn = {
    height: 362,
    autoLoad: false,
    pagination: false,
    onClickRow: function(row) {
        configDG_pcn_value = "";
        $(row.cells).each(function(k, v) {
            //console.log("columna: " + k + ", valor: " +  v.innerHTML);
            if (k == 1) {
                configDG_pcn_value += v.innerHTML + " ";
            } else if (k == 2) {
                configDG_pcn_value += v.innerHTML;
            } else if (k == 3) {
                configDG_pcn_value = v.innerHTML + ": " + configDG_pcc_value;
            }
            //$("#dialog-descripcion-concepto").html(configDG_pcc_value);
        });
    },
    mapper: [
        {name: 'id', title: '&nbsp;', width: "5%", align: 'center', render: function(v) {
                return "</a>&nbsp;<a href='#'><img src='ui/img/delete.png' onclick='eliminarFila_cn(" + v + "); return false;'/></a>";
            }},
        {name: 'codigo', title: 'CÓDIGO', width: "15%", align: "center"},
        {name: 'descripcion', title: 'DESCRIPCIÓN', width: "50%"},
        {name: 'debito', title: 'DÉBITO', width: "10%", align: "right"},
        {name: 'credito', title: 'CRÉDITO', width: "10%", align: "right"},
        {name: 'fecha', title: 'FECHA', width: "10%", align: "center"}
    ]
};

/**
 * 
 * @type GRID PANEL CONFIGURAR RECIBO INGRESO
 */
var configDG_pri_value = "";
var configDG_pri = {
    height: 234,
    autoLoad: false,
    pagination: false,
    onClickRow: function(row) {
        configDG_pri_value = "";
        $(row.cells).each(function(k, v) {
            //console.log("columna: " + k + ", valor: " +  v.innerHTML);
            if (k == 1) {
                configDG_pri_value += v.innerHTML + " ";
            } else if (k == 2) {
                configDG_pri_value += v.innerHTML;
            } else if (k == 3) {
                configDG_pri_value = v.innerHTML + ": " + configDG_pri_value;
            }
            //$("#dialog-descripcion-concepto").html(configDG_pcc_value);
        });
    },
    mapper: [
        {name: 'id', title: '&nbsp;', width: "5%", align: 'center', render: function(v) {
                return "</a>&nbsp;<a href='#'><img src='ui/img/delete.png' onclick='eliminarFila_ri(" + v + "); return false;'/></a>";
            }},
        {name: 'concepto', title: 'CONCEPTO', width: "75%"},
        {name: 'cantidad', title: 'CANTIDAD', width: "10%", align: "center"},
        {name: 'monto', title: 'VALOR UNITARIO', width: "10%", align: "right"}
    ]
};


/**
 * 
 * @type GRID PANEL CONFIGURAR ANULAR RECIBO INGRESO
 */
var configDG_par_value = "";
var configDG_par = {
    height: 359,
    autoLoad: false,
    pagination: false,
    onClickRow: function(row) {
        configDG_par_value = "";
        $(row.cells).each(function(k, v) {
            //console.log("columna: " + k + ", valor: " +  v.innerHTML);
            if (k == 1) {
                configDG_par_value += v.innerHTML + " ";
            }/* else if (k == 2) {
             configDG_par_value += v.innerHTML;
             } else if(k == 3) {
             configDG_par_value = v.innerHTML +": " + configDG_par_value;
             }*/
            $("#dialog-descripcion-recibo").html(configDG_par_value);
        });
    },
    mapper: [
        {name: 'id', title: '&nbsp;', width: "8%", align: 'center', render: function(v) {
                return "<a href='#'><img src='ui/img/delete.png' onclick='eliminarFila_ar(" + v + "); return false;'/></a>&nbsp;<a href='#'><img src='ui/img/descargar.png' onclick='imprimirReciboExcel(" + v + "); return false;'/></a>";
            }},
        {name: 'nrorecibo', title: 'NÚMERO DE RECIBO', width: "15%", align: "right"},
        {name: 'nrovoucher', title: 'NÚMERO DE VOUCHER', width: "15%", align: "left"},
        {name: 'persona', title: 'CLIENTE', width: "40%", align: "left"},
        {name: 'fecha', title: 'FECHA', width: "20%", align: "center"}
    ]
};

/**
 * 
 * @type GRID PANEL CONFIGURAR ORDEN COMPRA
 */
var configDG_poc_value = "";
var configDG_poc = {
    height: 178,
    autoLoad: false,
    pagination: false,
    onClickRow: function(row) {
        configDG_poc_value = "";
        $(row.cells).each(function(k, v) {
            //console.log("columna: " + k + ", valor: " +  v.innerHTML);
            if (k == 1) {
                configDG_poc_value += v.innerHTML + " ";
            }/* else if (k == 2) {
             configDG_par_value += v.innerHTML;
             } else if(k == 3) {
             configDG_par_value = v.innerHTML +": " + configDG_par_value;
             }*/
            //$("#dialog-descripcion-recibo").html(configDG_poc_value);
        });
    },
    mapper: [
        {name: 'id', title: '&nbsp;', width: "5%", align: 'center', render: function(v) {
                return "</a>&nbsp;<a href='#'><img src='ui/img/delete.png' onclick='eliminarFila_oc(" + v + "); return false;'/></a>";
            }},
        {name: 'concepto', title: 'CONCEPTO', width: "75%"},
        {name: 'cantidad', title: 'CANTIDAD', width: "10%", align: "center"},
        {name: 'monto', title: 'VALOR UNITARIO', width: "10%", align: "right"}
    ]
};

/**
 * 
 * @type GRID PANEL CONFIGURAR ORDEN DE COMPRA PENDIENTE DE COMPROBANTE DE PAGO
 */
var $configDG_ocpcp_value = "";
var configDG_ocpcp = {
    height: 391,
    autoLoad: false,
    pagination: false,
    onClickRow: function(row) {
        $configDG_ocpcp_value = "";
        $(row.cells).each(function(k, v) {
            //console.log("columna: " + k + ", valor: " +  v.innerHTML);
            if (k == 2) {
                $configDG_ocpcp_value = v.innerHTML;
            }/* else if (k == 2) {
             configDG_par_value += v.innerHTML;
             } else if(k == 3) {
             configDG_par_value = v.innerHTML +": " + configDG_par_value;
             }*/
            //$("#dialog-descripcion-recibo").html($configDG_ocpcp_value);
        });
    },
    mapper: [
        {name: 'id', title: '&nbsp;', width: "5%", align: 'center', render: function(v) {
                return "</a>&nbsp;<a href='#'><img src='ui/img/run24.png' onclick='mostrarComprobantePago(" + v + "); return false;'/></a>";
            }},
        {name: 'fecha', title: 'FECHA', width: "15%", align: "center"},
        {name: 'numeracion', title: 'NUMERACIÓN', width: "15%", align: "center"},
        {name: 'descripcion', title: 'DESCRIPCIÓN', width: "40%", align: "left"},
        {name: 'razonsocial', title: 'RAZON SOCIAL', width: "25%", align: "left"}
    ]
};


/**
 * 
 * @type GRID PANEL ANULAR ORDEN COMPRA Y COMPROBANTE DE PAGO
 */
var $configDG_aoccp_value = "";
var configDG_aoccp = {
    height: 375,
    autoLoad: false,
    pagination: false,
    onClickRow: function(row) {
        $configDG_aoccp_value = "";
        $(row.cells).each(function(k, v) {
            //console.log("columna: " + k + ", valor: " +  v.innerHTML);
            if (k == 1) {
                $configDG_aoccp_value += v.innerHTML + " ";
            }/* else if (k == 2) {
             configDG_par_value += v.innerHTML;
             } else if(k == 3) {
             configDG_par_value = v.innerHTML +": " + configDG_par_value;
             }*/
            $("#dialog-descripcion-orden").html($configDG_aoccp_value);
        });
    },
    mapper: [
        {name: 'id', title: '&nbsp;', width: "8%", align: 'center', render: function(v) {
                return "<a href='#'><img src='ui/img/delete.png' onclick='eliminarFila_ao(" + v + "); return false;'/></a>&nbsp;<a href='#'><img src='ui/img/descargar.png' onclick='imprimirOrdenExcel(" + v + "); return false;'/></a>";
            }},
        {name: 'nroorden', title: 'NRO ORDEN', width: "15%", align: "right"},
        {name: 'nrocomprobante', title: 'NRO COMP.', width: "15%", align: "right"},
        {name: 'persona', title: 'REPRESENTANTE', width: "25%", align: "left"},
        {name: 'empresa', title: 'EMPRESA', width: "25%", align: "left"},
        {name: 'fecha', title: 'FECHA', width: "12%", align: "center"}
    ]
};